//
//  ZCommentHeader.m
//  WeClubs
//
//  Created by Zed Link on 11/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZCommentHeader.h"
#import "UIButton+ZUtilities.h"
#import <Masonry.h>

@interface ZCommentHeader ()

@end

@implementation ZCommentHeader

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI{

    _allSelBtn = [UIButton zButtonWithTitle:@"全选" fontSize:15 textColor:HIGHTLIGHT_COLOR];
    _allSelBtn.hidden = YES;
    
    _allSelBtn.backgroundColor = [UIColor orangeColor];
 
    [self.contentView addSubview:_allSelBtn];
    Z_WEAK_SELF;
    [_allSelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(weakSelf.contentView).offset(-20);
        make.centerY.equalTo(weakSelf.contentView).offset(0);
        
    }];

}

- (void)zSetTitle:(NSString *)title
{
    [self.descriptionBtn setTitle:title forState:UIControlStateNormal];
}

- (void)zSetTitle:(NSString *)title andImage:(NSString *)imageName
{
    [self.descriptionBtn setTitle:title forState:UIControlStateNormal];
    [self.descriptionBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}

- (void)zSetAttributeTitle:(NSAttributedString *)title andImage:(NSString *)imageName
{
    [self.descriptionBtn setAttributedTitle:title forState:UIControlStateNormal];
    [self.descriptionBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}

- (void)zSetBackgroundColor:(UIColor *)backgroundColor
{
    UIView *bgView = [[UIView alloc] initWithFrame:self.contentView.bounds];
    [bgView setBackgroundColor:backgroundColor];
    [self setBackgroundView:bgView];
    [self.contentView setBackgroundColor:backgroundColor];
}

- (void)descriptionBtnAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(commentHeader:didSelected:)]) {
        [self.delegate commentHeader:self didSelected:sender];
    }
}

#pragma mark - Getter & Setter

@synthesize descriptionBtn = _descriptionBtn;
@synthesize separator = _separator;

- (UIButton *)descriptionBtn
{
    if (_descriptionBtn == nil) {
        _descriptionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_descriptionBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, 0)];
        [_descriptionBtn addTarget:self action:@selector(descriptionBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.contentView addSubview:_descriptionBtn];
    }
    return _descriptionBtn;
}

- (UIView *)separator
{
    if (_separator == nil) {
        _separator = [[UIView alloc] init];
        [_separator setBackgroundColor:SEPARATOR_BG_COLOR];
        [self.contentView addSubview:_separator];

        [_separator mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.left.right.bottom.mas_equalTo(self.contentView);
        }];
    }
    return _separator;
}

- (void)setStyle:(ZTableHeaderStyle)style
{
    _style = style;
    
    switch (_style) {
        case ZTableHeaderStyleComment:
        {
            [self.contentView setBackgroundColor:NAVBAR_BG_COLOR];
            [self.descriptionBtn setTitleColor:FONT_SECOND_COLOR forState:UIControlStateNormal];
            self.descriptionBtn.titleLabel.font = [UIFont systemFontOfSize:11];
        }
            break;
        case ZTableHeaderStyleSetting:
        {
            [self.contentView setBackgroundColor:NAVBAR_BG_COLOR];
            [self.descriptionBtn setTitleColor:FONT_SECOND_COLOR forState:UIControlStateNormal];
            self.descriptionBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        }
            break;
        case ZTableHeaderStyleDetail:
        {
            [self.contentView setBackgroundColor:[UIColor clearColor]];
            [self.descriptionBtn setTitleColor:FONT_THIRD_COLOR forState:UIControlStateNormal];
            self.descriptionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        }
            break;
        case ZTableHeaderStyleAdd:
        {
            [self.contentView setBackgroundColor:NAVBAR_BG_COLOR];
            [self.descriptionBtn setTitleColor:HIGHTLIGHT_COLOR forState:UIControlStateNormal];
            self.descriptionBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        }
            break;
        default:
            break;
    }
}

- (void)setAlignment:(ZTableHeaderAlignment)alignment
{
    _alignment = alignment;
    
    switch (_alignment) {
        case ZTableHeaderAlignmentTop:
        {
            self.descriptionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [self.descriptionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.contentView).offset(12);
                make.right.mas_equalTo(self.contentView);
                
                make.top.mas_equalTo(self.contentView.mas_top).offset(0);
                make.height.mas_equalTo(30);
            }];
        }
            break;
        case ZTableHeaderAlignmentCenter:
        {
            self.descriptionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [self.descriptionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.contentView).offset(12);
                make.right.centerY.mas_equalTo(self.contentView);
                
                
            }];
        }
            break;
        case ZTableHeaderAlignmentFill:
        {
            self.descriptionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [self.descriptionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self.contentView);
                make.top.bottom.mas_equalTo(self.contentView);
                make.width.mas_equalTo(128);
            }];
        }
            break;
        default:
            break;
    }
}

@end
